package com.yuyang.wifi.views;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.yuyang.wifi.MyApplication;
import com.yuyang.wifi.utils.DeviceInfoUtils;
import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.interstitial2.UnifiedInterstitialAD;
import com.qq.e.ads.interstitial2.UnifiedInterstitialADListener;
import com.qq.e.ads.interstitial2.UnifiedInterstitialMediaListener;
import com.qq.e.comm.constants.AdPatternType;
import com.qq.e.comm.util.AdError;

import java.util.Locale;

/**
 * created by mej
 * on 2020/12/2
 */
public class LoadYlhInsertAdUtils implements UnifiedInterstitialADListener, UnifiedInterstitialMediaListener {

    private Context mContext;
    private OnItemAllClickListener onItemAllClickListener;
    private UnifiedInterstitialAD iad;
    private static final String TAG = "ylh_insert";
    private String mYlhInsertAdId;

    static LoadYlhInsertAdUtils instance = new LoadYlhInsertAdUtils();

    public static LoadYlhInsertAdUtils getInstance() {
        return instance;
    }

    //一、加载穿山甲激励视频
    //BDAdvanceBaseAppNative 绑定激励视频回调
    public void load(final Context context, String moduleName) {
        mContext = context;
//        String channelName = DeviceInfoUtils.getUmChannelName(mContext);
//        if (!TextUtils.isEmpty(channelName) && "sykq000001".equals(channelName)) {
//            mYlhInsertAdId = "6071780845893434";
//        } else if (!TextUtils.isEmpty(channelName) && "sykq000002".equals(channelName)) {
//            mYlhInsertAdId = "6071780845893434";
//        } else {
//            mYlhInsertAdId = "3001275373920889";
//        }
        mYlhInsertAdId = MyApplication.getYlhChannel(moduleName);
        initAd();
    }

    private void initAd() {
        try {
            if (this.iad != null) {
                iad.close();
                iad.destroy();
                iad = null;
            }
            iad = new UnifiedInterstitialAD((Activity) mContext, mYlhInsertAdId, this);
            iad.setVideoPlayPolicy(VideoOption.VideoPlayPolicy.AUTO);
            iad.loadAD();

            VideoOption.Builder builder = new VideoOption.Builder();
            VideoOption option = builder.build();
            option = builder.setAutoPlayMuted(false)
                    .build();
            iad.setVideoOption(option);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onADReceive() {
        //Toast.makeText(mContext, "广告加载成功 ！ ", Toast.LENGTH_LONG).show();
        // onADReceive之后才能调用getAdPatternType()
        if (iad.getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
            iad.setMediaListener(this);
        }
        // onADReceive之后才可调用getECPM()
        Log.d(TAG, "eCPMLevel = " + iad.getECPMLevel());
        // iad.show();
        if (iad != null) {
            iad.show();
        } else {
            //Toast.makeText(mContext, "请加载广告后再进行展示 ！ ", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onVideoCached() {
        // 视频素材加载完成，在此时调用iad.show()或iad.showAsPopupWindow()视频广告不会有进度条。
        Log.i(TAG, "onVideoCached");
    }

    @Override
    public void onNoAD(AdError error) {
        String msg = String.format(Locale.getDefault(), "onNoAD, error code: %d, error msg: %s",
                error.getErrorCode(), error.getErrorMsg());
        //  Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();


    }

    @Override
    public void onADOpened() {
        Log.i(TAG, "onADOpened");
    }

    @Override
    public void onADExposure() {
        Log.i(TAG, "onADExposure");
    }

    @Override
    public void onADClicked() {
        Log.i(TAG, "onADClicked : " + (iad.getExt() != null ? iad.getExt().get("clickUrl") : ""));
    }

    @Override
    public void onADLeftApplication() {
        Log.i(TAG, "onADLeftApplication");
    }

    @Override
    public void onADClosed() {
        Log.i(TAG, "onADClosed");
        if (null != onItemAllClickListener) {
            onItemAllClickListener.onItemAllClickListener();
        }
    }

    @Override
    public void onVideoComplete() {

    }

    @Override
    public void onVideoError(AdError adError) {

    }

    @Override
    public void onVideoInit() {

    }

    @Override
    public void onVideoLoading() {

    }

    @Override
    public void onVideoPageClose() {

    }

    @Override
    public void onVideoPageOpen() {

    }

    @Override
    public void onVideoPause() {

    }

    @Override
    public void onVideoReady(long l) {

    }

    @Override
    public void onVideoStart() {

    }


    public void showInterActionAdVideo() {
        if (iad != null) {
            iad.show();
        } else {
            Toast.makeText(mContext, "请加载广告后再进行展示 ！ ", Toast.LENGTH_LONG).show();
        }
    }


    public interface OnItemAllClickListener {
        void onItemAllClickListener();
    }

    public void setItemAllClickListener(OnItemAllClickListener onItemAllClickListener) {
        this.onItemAllClickListener = onItemAllClickListener;
    }

}
